﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace SampleStockWP.Views
{
    public partial class StockVN : PhoneApplicationPage
    {
        StockServiceReference.Service1Soap asyncWS = new StockServiceReference.Service1SoapClient();
        TickerVN result = new TickerVN();
        ProgressBar bar = new ProgressBar();

        public StockVN()
        {
            InitializeComponent();

            List<String> tickerList = new List<String>
            {
                "AAM","ABT","ACL","AGD","AGF","AGR","ALP","ANV","APC","ASM","ASP","ATA","AVF",
                "BAS","BBC","BCE","BCI","BHS","BMC","BMI","BMP","BT6","BTP","BTT","BVH",
                "C47","CAD","CCI","CCL","CDC","CII","CLC","CLG","CLW","CMG","CMT","CMV","CMX","CNT","COM","CSG","CSM","CTD","CTG","CTI","CYC",
                "D2D","DAG","DCC","DCL","DCT","DDM","DHA","DHC","DHG","DIC","DIG","DLG","DMC","DPM","DPR","DQC","DRC","DRH","DSN","DTA","DTL","DTT","DVD","DVP","DXG","DXV",
                "EIB","ELC","EVE","FBT","FDC","FMC","FPC","FPT","GDT","GIL","GMC","GMD","GTA","GTT",
                "HAG","HAI","HAP","HAS","HAX","HBC","HCM","HDC","HDG","HLA","HLG","HMC","HPG","HQC","HRC","HSG","HSI","HT1","HTI","HTL","HTV","HVG","HVX",
                "ICF","IFS","IJC","IMP","ITA","ITC","KAC","KBC","KDC","KDH","KHA","KHP","KMR","KSA","KSB","KSH","KSS","KTB","L10","LAF","LBM","LCG","LGC","LGL","LHG","LIX","LM8","LSS",
                "MAFPF1","MCG","MCP","MCV","MDG","MHC","MKP","MPC","MSN","MTG","NAV","NBB","NHS","NHW","NKG","NNC","NSC","NTB","NTL","NVN","NVT","OGC","OPC",
                "PAC","PAN","PDR","PET","PGC","PGD","PHR","PHT","PIT","PJT","PNC","PNJ","POM","PPC","PPI","PRUBF1","PTC","PTL","PVD","PVF","PVT","PXI","PXL","PXM","PXS","PXT","QCG",
                "RAL","RDP","REE","RIC","SAM","SAV","SBA","SBC","SBS","SBT","SC5","SCD","SEC","SFC","SFI","SGT","SHI","SJD","SJS","SMA","SMC","SPM","SRC","SRF","SSC","SSI","ST8","STB","STG","SVC","SZL",
                "TAC","TBC","TCL","TCM","TCR","TDC","TDH","TDW","TIC","TIE","TIX","TLG","TLH","TMP","TMS","TMT","TNA","TNC","TNT","TPC","TRA","TRC","TRI","TS4","TSC","TTF","TTP","TV1","TYA",
                "UDC","UIC","VCB","VCF","VES","VFC","VFG","VFMVF1","VFMVF4","VFMVFA","VHC","VHG","VIC","VID","VIP","VIS","VKP","VLF","VMD","VNA","VNE","VNG","VNH","VNI","VNL","VNM",
                "VNS","VOS","VPH","VPK","VPL","VRC","VSC","VSG","VSH","VSI","VST","VTB","VTF","VTO"
            };

            this.autoCompleteBox1.ItemsSource = tickerList;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.autoCompleteBox1.Text.Length > 0)
                {
                    bar.IsIndeterminate = true;

                    this.ContentPanel.Children.Add(bar);

                    String ticker = this.autoCompleteBox1.Text;
                    String date = DateTime.Today.ToShortDateString(); // mm/dd/yyyy

                    AsyncCallback cb = new AsyncCallback(ServiceCallback);
                    asyncWS.BeginGetTickerByDate(ticker, date, cb, asyncWS);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error! Please try again later.", "Web Service is busy", MessageBoxButton.OK);
            }
        }

        public void ServiceCallback(IAsyncResult ar)
        {
            try
            {
                StockServiceReference.Service1Soap ws = (StockServiceReference.Service1SoapClient)ar.AsyncState;
                SampleStockWP.StockServiceReference.ArrayOfXElement temp = ws.EndGetTickerByDate(ar);
                if (temp != null)
                {
                    String soapResponse = temp.Nodes[0].ToString();
                    if (soapResponse.Length > 0)
                    {
                        result.ticker = soapResponse.Substring(soapResponse.IndexOf("<ticker>") + 8, soapResponse.IndexOf("</ticker>") - (soapResponse.IndexOf("<ticker>") + 8));
                        result.date = soapResponse.Substring(soapResponse.IndexOf("<date>") + 6, 10);
                        result.open_price = soapResponse.Substring(soapResponse.IndexOf("<open_price>") + 12, soapResponse.IndexOf("</open_price>") - (soapResponse.IndexOf("<open_price>") + 12));
                        result.high_price = soapResponse.Substring(soapResponse.IndexOf("<high_price>") + 12, soapResponse.IndexOf("</high_price>") - (soapResponse.IndexOf("<high_price>") + 12));
                        result.low_price = soapResponse.Substring(soapResponse.IndexOf("<low_price>") + 11, soapResponse.IndexOf("</low_price>") - (soapResponse.IndexOf("<low_price>") + 11));
                        result.close_price = soapResponse.Substring(soapResponse.IndexOf("<close_price>") + 13, soapResponse.IndexOf("</close_price>") - (soapResponse.IndexOf("<close_price>") + 13));
                        result.volume = soapResponse.Substring(soapResponse.IndexOf("<volume>") + 8, soapResponse.IndexOf("</volume>") - (soapResponse.IndexOf("<volume>") + 8));
                    }
                }

                //Update UI
                Action _onUpdateAction = new Action(UpdateTicker);
                Dispatcher.BeginInvoke(_onUpdateAction);
            }
            catch (Exception e)
            {
                MessageBox.Show("Error! Please try again later.", "Web Service is busy", MessageBoxButton.OK);
            }
        }

        protected void UpdateTicker()
        {
            this.txtTicker.Text = result.ticker;
            this.txtDate.Text = result.date;
            this.txtOpen.Text = result.open_price;
            this.txtHigh.Text = result.high_price;
            this.txtLow.Text = result.low_price;
            this.txtClose.Text = result.close_price;
            this.txtVolume.Text = result.volume;

            this.ContentPanel.Children.Remove(bar);
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/Views/ChartVN.xaml?opt={0}&sym={1}", 1 , this.autoCompleteBox1.Text), UriKind.RelativeOrAbsolute));
        }

    }
}